/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simplepushserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Filip
 */
public class SettingsHandler{
    private static File settingsFile = new File(System.getenv("APPDATA"),"PushSettings.ini");
    private Settings settingsObject;

    public SettingsHandler() {
        if(settingsFile.exists() && settingsFile.canRead()){
            try{
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(settingsFile));
                Object o = ois.readObject();
                settingsObject = (Settings)o;
            }catch(Exception e){
                settingsFile.delete();
                settingsObject = new Settings();
            }
        }else{
            settingsObject = new Settings();
        }
        saveFile();
    }

    public synchronized void saveFile(){
        ObjectOutputStream oos = null;
        try {
            settingsFile.delete();
            oos = new ObjectOutputStream(new FileOutputStream(settingsFile));
            oos.writeObject(settingsObject);
            System.out.println("Saving...");
        }catch (IOException ex){
            ex.printStackTrace();
        }finally{
            try {
                oos.close();
            } catch (IOException ex) {}
        }

    }
    
    public synchronized ArrayList<File> getMediaFolders() {
        return settingsObject.mediaFolders;
    }
    public synchronized void addMediaFolder(File folder) {
        settingsObject.mediaFolders.add(folder);
        saveFile();
    }
    public synchronized void removeMediaFolder(int index) {
        settingsObject.mediaFolders.remove(index);
        saveFile();
    }
    public synchronized int getServerPort() {
        return settingsObject.port;
    }
    public synchronized void setServerPort(int port) {
        settingsObject.port = port;
        notifyAll();
        saveFile();
    }
    public synchronized String getStartMessage() {
        return settingsObject.startMessage;
    }
    public synchronized void setStartMessage(String startMessage) {
        settingsObject.startMessage = startMessage;
        saveFile();
    }
    
    /**
     * @return the nirCmdFile
     */
    public synchronized File getNirCmdFile() {
        return settingsObject.nirCmdFile;
    }
    /**
     * @param nirCmdFile the nirCmdFile to set
     */
    public synchronized void setNirCmdFile(File nirCmdFile) {
        settingsObject.nirCmdFile = nirCmdFile;
    }
    /**
     * @return the winampPath
     */
    public synchronized File getMediaPlayPath() {
        return settingsObject.mediaPlayPath;
    }

    /**
     * @param winampPath the winampPath to set
     */
    public synchronized void setMediaPlayPath(File winampPath) {
        settingsObject.mediaPlayPath = winampPath;
    }

    public synchronized String getMediaEnqueueParam() {
        return settingsObject.mediaEnqueueParam;
    }

    public synchronized void setMediaEnqueueParam(String winampEnqueueParam) {
        settingsObject.mediaEnqueueParam = winampEnqueueParam;
    }
    @Override
    protected void finalize() throws Throwable {
        saveFile();
        super.finalize();
    }

    
}
class Settings implements Serializable{
        protected int port;
        protected String startMessage;
        protected ArrayList<File> mediaFolders;
        protected File nirCmdFile;
        protected File mediaPlayPath;
        protected String mediaEnqueueParam;
        public static final long serialVersionUID = 1;


        public Settings() {
            port = 1337;
            startMessage = "Server running!";
            mediaFolders = new ArrayList<File>();
            nirCmdFile = new File("C:\\nircmd.exe");
            mediaPlayPath = new File("C:\\Program Files (x86)\\Winamp\\winamp.exe");
            mediaEnqueueParam = "/ADD [filename]";
        }

        public String toString(){
            StringBuilder sb = new StringBuilder(startMessage);
            sb.append('\n');
            for(File f:mediaFolders){
                sb.append(f.getAbsolutePath());
                sb.append('\n');
            }
            return sb.toString();
        }
}
